package ext.opensource.netty.server.httpsocket;

import java.nio.ByteBuffer;

import ext.opensource.netty.common.BaseSocketSession;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

public class WebSocketSession extends BaseSocketSession {
	private static final long serialVersionUID = 1L;

	public WebSocketSession(final Channel channel) {
		super(channel);
		// TODO Auto-generated constructor stub
	}

	@Override
	public ChannelFuture sendTextMessage(final String message) {
		return channel().writeAndFlush(new TextWebSocketFrame(message));
	}

	public ChannelFuture sendTextMessage(final ByteBuf byteBuf) {
		return channel().writeAndFlush(new TextWebSocketFrame(byteBuf));
	}

	public ChannelFuture sendTextMessage(final ByteBuffer byteBuffer) {
		final ByteBuf buffer = channel().alloc().buffer(byteBuffer.remaining());
		buffer.writeBytes(byteBuffer);
		return channel().writeAndFlush(new TextWebSocketFrame(buffer));
	}

	public ChannelFuture sendBinaryMessage(final byte[] bytes) {
		final ByteBuf buffer = channel().alloc().buffer(bytes.length);
		return channel().writeAndFlush(new BinaryWebSocketFrame(buffer.writeBytes(bytes)));
	}

	@Override
	public ChannelFuture sendBinaryMessage(final ByteBuf byteBuf) {
		return channel().writeAndFlush(new BinaryWebSocketFrame(byteBuf));
	}

	public ChannelFuture sendBinaryMessage(final ByteBuffer byteBuffer) {
		final ByteBuf buffer = channel().alloc().buffer(byteBuffer.remaining());
		buffer.writeBytes(byteBuffer);
		return channel().writeAndFlush(new BinaryWebSocketFrame(buffer));
	}
}
